﻿using System.Linq.Expressions;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;

namespace XLZF.MicroServices.Repository;

public class UserRepository: IUserRepository
{
    private static ModelMigration.XLZFContext context = new ModelMigration.XLZFContext();

    private IRepository<Clothing_Users> _repository = new Repository<Clothing_Users>(context);

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="Clothing_Users"></param>
    /// <returns></returns>
    public async Task<int> AddClothingUsers(Clothing_Users Clothing_Users)
    {
        return await _repository.Add(Clothing_Users);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="Clothing_Users"></param>
    /// <returns></returns>
    public int DelClothingUsers(Clothing_Users Clothing_Users)
    {
        return _repository.Remove(Clothing_Users);
    }

    /// <summary>
    /// 改
    /// </summary>
    /// <param name="Clothing_Users"></param>
    /// <returns></returns>
    public int EditClothingUsers(Clothing_Users Clothing_Users)
    {
        return _repository.Update(Clothing_Users);
    }

    /// <summary>
    /// 查
    /// </summary>
    /// <returns></returns>
    public List<Clothing_Users> GetClothingUsers()
    {
        return _repository.GetAll().ToList();
    }

    /// <summary>
    /// 查
    /// </summary>
    /// <param name="UserID"></param>
    /// <returns></returns>
    public Clothing_Users GetClothingUsersSing(int UserID)
    {
        return _repository.GetModel(p => p.UserID == UserID);
    }

    /// <summary>
    /// 条件查询
    /// </summary>
    /// <param name="name">姓名</param>
    /// <param name="isAsc">是否正序</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">每页大小</param>
    /// <returns></returns>
    public Page<Clothing_Users> GetClothingPageData(string name, bool isAsc, int pageIndex, int pageSize)
    {
        Page<Clothing_Users>? data = null;

        if (string.IsNullOrEmpty(name))
        {
            data = _repository.SearchFor(pageIndex, pageSize, null, isAsc, e => e.CreateDate);
        }
        else
        {
            //条件表达式
            Expression<Func<Clothing_Users, bool>> predicate = e => string.IsNullOrWhiteSpace(name) || e.UserName.Equals(name);

            data = _repository.SearchFor(pageIndex, pageSize, predicate, isAsc, e => e.CreateDate);
        }

        return data;
    }
}
